
package coms.dataaccess.hibernate;

import coms.dataaccess.dao.UserDAO;
import coms.dto.*;
import java.sql.SQLException;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import java.util.List;
import org.hibernate.criterion.Projections;

public class UserDAOImpl extends GenericDAOImpl<User, Integer> implements UserDAO{

    private Session session;
    
    @Override
    public User findUserByUsername(String username) {
    // BAD CODE => NEED TO CHANGE (FOR TESTING ONLY)
    session = getSession();
    Criteria criteria = session.createCriteria(User.class)
    .add(Restrictions.eq("username", username));
    List<User> userList = criteria.list();
    clear();
    if (userList.size()>0)
        return userList.get(0);
    else 
        return null;
        
        
    }

    @Override
    public List<User> searchParticipantByConference(Conference conference) {
        session = HibernateUtil.getSessionFactory().openSession();
          Criteria criteria = session.createCriteria(ConferenceRegistration.class, "confReg");
        if (conference!= null){
            criteria.createAlias("confReg.conference", "c")
                    .add(Restrictions.eq("c.id", conference.getId()));
        }
        criteria.setProjection(Projections.property("confReg.participant"));
     
        return criteria.list();
    }
    
    @Override
    public List<User> searchParticipantByConferenceItem(ConferenceItem conferenceItem) {
             session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(ConferenceItemBooking.class,"confItemBooking");
        if (conferenceItem!= null){
                criteria.createAlias("confItemBooking.conferenceItem", "ci")
                        .add(Restrictions.eq("ci.id", conferenceItem.getId()));
        }
         criteria.setProjection(Projections.property("confItemBooking.participant"));
        return criteria.list();
        }

    @Override
    public List<User> search(User user) throws SQLException {
       session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(User.class, "user")
                .createAlias("user.profile", "pro");
        
        if (user == null) return criteria.list();
        
         if (user.getEmailAddress()!=null && !user.getEmailAddress().equals("")){
            criteria.add(Restrictions.like("user.emailAddress", "%"+user.getEmailAddress()+"%"));
        }
         
         if (user.getUsername()!=null && !user.getUsername().equals("")){
            criteria.add(Restrictions.like("user.username", "%"+user.getUsername()+"%"));
        }
        
        if (user.getProfile() != null && user.getProfile().getLastName()!=null && !user.getProfile().getLastName().equals("")){
                   criteria.add(Restrictions.like("pro.lastName", "%"+user.getProfile() .getLastName()+"%"));
        }
        
        if (user.getProfile() != null && user.getProfile().getFirstName()!=null && !user.getProfile().getFirstName().equals("")){
            criteria.add(Restrictions.like("pro.firstName", "%"+user.getProfile() .getFirstName()+"%"));
        }
        
        List<User> userList = criteria.list();
        clear();
        return userList;
    }
    
    }
